package com.rover.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.Common;
import com.rover.service.system.AccountService;
import com.rover.service.system.ResourceService;
import com.rover.service.system.RoleResourceService;
import com.rover.service.system.RoleService;
import com.rover.service.system.SysParamService;

/**
 * 初始化资源
 */
@Controller
@RequestMapping("/repairController")
public class RepairController {
    private static Logger logger = LoggerFactory.getLogger(RepairController.class);

    public static final String COOK_USERNAME = "OOUsername";
    public static final String COOK_PASSWORD = "OOPassword";

    @Autowired
    private ResourceService resourcService; 
    
    @Autowired
    private AccountService accountManagerI;
    
    @Autowired
    private RoleResourceService roleResourceService;
    
    @Autowired
    private SysParamService sysParamService;
    
    @Autowired
    private RoleService roleService;

    @RequestMapping("/repair")
    public @ResponseBody
    Map<String, Object> initData(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", true);

        List<Bean> data = new ArrayList<Bean>();

        Logger dblogger = LoggerFactory.getLogger(Common.DB_LOGGER_NAME);

        int width = 30;
        String title = "开始初始化系统基础数据";
        String stars = StringUtils.repeat("*", width);
        String centered = StringUtils.center(title, width, "*");
        String heading = StringUtils.join(new Object[] { stars, centered, stars }, "\n");

        logger.info(heading + "{}", new Object[] { new Date() });
        dblogger.info("开始初始化系统基础数据...");

        long start = System.currentTimeMillis();
        accountManagerI.initData();

        try {
        	roleService.initData();
            data.add(new Bean(true, "角色初始数据成功!", this.accountManagerI.getClass().getName()));

        } catch (Exception se) {
            logger.error("cuco ...");
            data.add(new Bean(false, "角色初始数据失败!" + se.toString(), ""));
        }

        try {
            resourcService.initData(this.getClass().getResource("/data/resource.txt").getFile());
            data.add(new Bean(true, "资源初始数据成功!", this.resourcService.toString()));
        } catch (Exception se) {
            logger.error("cuco ...");
            data.add(new Bean(false, "资源初始数据失败!" + se.toString(), this.resourcService.toString()));
        }
        
        try {
        	roleResourceService.initData();
            data.add(new Bean(true, "角色菜单初始数据成功!", this.resourcService.toString()));
        } catch (Exception se) {
            logger.error("cuco ...");
            data.add(new Bean(false, "角色菜单初始数据失败!" + se.toString(), this.resourcService.toString()));
        }
        
        try {
        	sysParamService.initData();
            data.add(new Bean(true, "系统参数初始数据成功!", this.sysParamService.toString()));
        } catch (Exception se) {
            logger.error("cuco ...");
            data.add(new Bean(false, "系统参数初始数据失败!" + se.toString(), this.resourcService.toString()));
        }
        
        logger.info(" 执行共计:" + (System.currentTimeMillis() - start) + " ms");
        dblogger.info(" 执行共计:" + (System.currentTimeMillis() - start) + " ms");

        map.put("datas", data);
        map.put("times", (System.currentTimeMillis() - start) + " ms");
        map.put("totalCount", data.size());
        return map;
    }

    public static class Bean {
        private boolean success;
        private String msg;
        private String name;
        private String notes;

        public String getNotes() {
            return notes;
        }

        public void setNotes(String notes) {
            this.notes = notes;
        }

        public Bean(boolean success, String msg, String info) {
            this.success = success;
            this.msg = msg;
            this.name = info;
        }

        public boolean getSuccess() {
            return success;
        }

        public String getMsg() {
            return msg;
        }

        public String getName() {
            return name;
        }
    }

}
